﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tao.OpenGl;
using PocketEngine.Engine;
namespace PocketEngine.Effects
{
    public class pShader
    {
        public string Path
        {
            get
            {
                return mPath;
            }
            set
            {
                mPath = value;
            }
        }
        private string mPath = string.Empty;

        public int ShaderHandle
        {
            get
            {
                return mShaderHandle;
            }
            set
            {
                mShaderHandle = value;
            }
        }
        private int mShaderHandle;
        public void LoadShader()
        {
            FileStream fs = new FileStream(Path,FileMode.Open,FileAccess.Read);
            TextReader tr = new StreamReader(fs);

            int length = 0;
            string[] lines = new string[1];
            lines[0] = tr.ReadToEnd() + "\n";
            length = lines[0].Length;
            Gl.glShaderSource(mShaderHandle, 1, lines,ref length);
            tr.Close();
        }
        public void CompileShader()
        {
            Gl.glCompileShader(mShaderHandle);
            
           
            int mlen = 0;
            int status = 0;
            Gl.glGetShaderiv(mShaderHandle, Gl.GL_COMPILE_STATUS, out status);
            if (status == Gl.GL_FALSE)
            {
                StringBuilder logb = new StringBuilder(mlen);
                int alen = 0;
                Gl.glGetShaderInfoLog(mShaderHandle, 1024 * 100, out alen, logb);
                pEngine.Err("Shader Error. Path:" + Path + "\n" + logb.ToString(), "pShader.CompileShader");
            }
            else
            {
                Console.WriteLine("Succesfully compiled shader:" + Path);
            }

        }
    }
}
